﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Simply.BusinessTier.Facade.Interface;
using Simply.BusinessTier.PDF;
using Simply.BusinessTier.FileStorage;
using Domain;
using Simply.DataTier.Repository.Implementation;
using Simply.DataTier.Repository.Interface;
using Simply.Domain;



namespace Simply.BusinessTier.Facade.Implementation
{
    public class ProductService : IProductService
    {

        private IProductRepository productRepository;
        public ProductService(IProductRepository productRepository)
        {
            this.productRepository = productRepository;
        }

        public ProductService()
            : this( new ProductEFRepository())
        {
        }


        #region IProductService Members

        public long? CreatePackage(PackageDO packageDo)
        {
            packageDo.Product = this.GetProductById(packageDo.Product.Id);
            long? packId = null;
            try
            {
                packId = productRepository.CreatePackage(packageDo);
            }
            catch (Exception ex)
            {
                //Ignore
            }

            if (packId.HasValue)
            {
                string pdfFilePath = FileStoreService.GetPDFStoreFilePath(packId.Value, packageDo.Product.TypeKey);
                PDFService pdfService = new PDFService();
                pdfService.GeneratePDF(packageDo.InfoData, packageDo.Product.Id, pdfFilePath);

                productRepository.UpdatePackageFilePath(packId.Value, FileStoreService.GetPDFStoreDirPath(pdfFilePath));
                return packId.Value;
            }
            return null;
        }

        public PrintMediaDO GetDefaultPrintMediaByProductId(long productId)
        {
            return productRepository.GetDefaultPrintMediaByProductId(productId);
        }

        public List<PrintMediaDO> GetMediaListByProductId(long productId)
        {
            return productRepository.GetMediaListByProductId(productId);
        }

        public List<ProductDO> GetAllProducts(string productType)
        {
            return productRepository.GetAllProducts(productType);
        }

        public List<ProductDO> GetFilteredProducts(ProductFilter filter)
        {
            return productRepository.GetFilteredProducts(filter);
        }

        public int GetFilteredProductsCount(ProductFilter filter)
        {
            return productRepository.GetFilteredProductsCount(filter);
        }

        public DataTemplateDO GetTemplateById(int id)
        {
            return productRepository.GetTemplateById(id);
        }

        public List<DataTemplateDO> GetTemplatesByProductId(long productId)
        {
            return productRepository.GetTemplatesByProductId(productId);
        }

        public ProductDO GetProductById(long id)
        {
            return productRepository.GetById(id);
        }


        public void UpdatePackageFilePath(long packageId, string path)
        {
            productRepository.UpdatePackageFilePath(packageId, path);
        }

        public PrintMethodDO GetDefaultPrintMethodByMediaCode(string mediaCode)
        {
            throw new NotImplementedException();
        }

        public int CreateTemplate(DataTemplateDO template)
        {
            return productRepository.CreateTemplate(template);
        }
        public IEnumerable<DataTemplateDO> GetAllTemplates(string productType)
        {
            return productRepository.GetAllTemplates(productType);
        }

        #endregion
    }
}
